<?php
namespace App\Http\Controllers;

use App\DBs\AdminDB;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    /**
     * 基本控制器
     */

    public function __construct()
    {
    }

    /**
     * 返回WEB端的Json数据
     * @param $code 错误代码
     * @param $data 返回的数据
     * return json
     */
    public function toAppJson($code=0, $data=array())
    {
        $msgArr = array(
            0 => '操作成功',
            -1 => '接口权限错误', '没有登陆', '参数错误',
            -4 => '没有数据', '新增失败', 'TOKEN失败',
            -7 => '管理员不存在', '密码错误', '更新失败',
            -10 => '密码格式错误', '', '',
            -13 => '', '', '',
            -500 => '操作失败',
        );
        $msgStr = array_key_exists($code,$msgArr) ? $msgArr[$code] : '操作失败';
        $jsonArr = array(
            'code' => $code,
            'msg' => $msgStr,
        );
        if ($data) { $jsonArr['data'] = $data; }
//        $this->setReqLog($jsonArr);
        return $jsonArr;
    }

    /**
     * 通过TOKEN，获取管理员ID
     */
    public function getAdminByToken($token)
    {
        $adminOne = new AdminDB();
        $resOne = $adminOne->getAdminByToken($token);
        if ($resOne['code']!=0) { return ''; }
        return $resOne['data']['adminId'];
    }

    /**
     * 通过TOKEN，获取管理员身份
     */
    public function getAdminTypeByToken($token)
    {
        $adminOne = new AdminDB();
        $resOne = $adminOne->getAdminByToken($token);
        if ($resOne['code']!=0) { return ''; }
        return $resOne['data']['genre'];
    }

    /**
     * 通过TOKEN，获取管理员信息
     */
    public function getAdminUnfoByToken($token)
    {
        $adminOne = new AdminDB();
        $resOne = $adminOne->getAdminByToken($token);
        if ($resOne['code']!=0) { return ''; }
        return $resOne['data'];
    }

    /**
     * 通过UID，获取TOKEN
     */
    public function setTokenByAdminid($adminId)
    {
        $adminOne = new AdminDB();
        $resOne = $adminOne->setTokenByAdminid($adminId);
        if ($resOne['code']!=0) { return 0; }
        return $resOne['data']['token'];
    }
}
